package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.SPNursingHome.dao.BedRecordDao;
import com.neusoft.SPNursingHome.po.ClientCheckInDto;
import com.neusoft.SPNursingHome.dao.BedDao;
import com.neusoft.SPNursingHome.dao.CheckInRecordDao;
import com.neusoft.SPNursingHome.dao.ClientDao;
import com.neusoft.SPNursingHome.po.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CheckInRecordServiceImpl implements CheckInRecordService {
    @Resource
    private ClientDao clientDao;
    @Resource
    private CheckInRecordDao checkInRecordDao;
    @Resource
    private BedDao bedDao;
    @Resource
    private BedRecordDao bedRecordDao;

    @Override
    @Transactional
    public ResponseBean<Void> registerClient(ClientCheckInDto dto) {
        try {

            // 身份证号重复性验证
            String idCardNumber = dto.getClientCardNumber();
            if (idCardNumber != null && !idCardNumber.trim().isEmpty()) {
                QueryWrapper<Client> wrapper = new QueryWrapper<>();
                wrapper.eq("client_card_number", idCardNumber);
                if (clientDao.selectCount(wrapper) > 0) {
                    // 如果找到重复项，立即返回一个明确的错误信息，不再继续执行
                    return new ResponseBean<>(400, "登记失败：该身份证号已被注册！");
                }
            }
            // 业务校验
            if (dto.getBedId() == null) throw new IllegalArgumentException("必须选择一个床位！");
            if (dto.getContractEndTime().before(dto.getCheckInTime())) throw new IllegalArgumentException("合同到期时间不能早于入住时间！");

            // 验证床位是否仍可用
            Bed bed = bedDao.selectById(dto.getBedId());
            if (bed == null || !"空闲".equals(bed.getBedStatus())) throw new RuntimeException("该床位已被占用，请重新选择！");

            // 1. 新增客户 Client
            Client client = new Client();
            client.setClientName(dto.getClientName());
            client.setClientType(dto.getClientType());
            client.setClientAge(dto.getClientAge());
            client.setDateOfBirth(dto.getDateOfBirth());
            client.setClientGender(dto.getClientGender());
            client.setClientCardNumber(dto.getClientCardNumber());
            client.setBloodType(dto.getBloodType());
            client.setClientPhone(dto.getClientPhone());
            client.setFamilyMember(dto.getFamilyMember());
            client.setBedId(dto.getBedId());
            client.setAdId(dto.getAdId());
            client.setClientStatus(1);
            clientDao.insert(client);

            Integer newClientId = client.getClientId();

            // 2. 新增入住记录 CheckInRecord
            CheckInRecord checkInRecord = new CheckInRecord();
            checkInRecord.setClientId(newClientId);
            checkInRecord.setCheckInTime(dto.getCheckInTime());
            checkInRecord.setContractEndTime(dto.getContractEndTime());
            checkInRecord.setAdId(dto.getAdId());
            checkInRecordDao.insert(checkInRecord);

            // 3. 新增床位使用记录 BedRecord
            BedRecord bedRecord = new BedRecord();
            bedRecord.setClientId(newClientId);
            bedRecord.setBedId(dto.getBedId());
            bedRecord.setBedBeginTime(dto.getCheckInTime());
            bedRecord.setBedRecordStatus(1);
            bedRecordDao.insert(bedRecord);

            // 4. 更新床位状态 Bed
            bed.setBedStatus("有人");
            bedDao.updateById(bed);

            return new ResponseBean<>(200, "客户入住登记成功");
        } catch (Exception e) {
            // 抛出运行时异常，让Spring的事务管理器回滚事务
            throw new RuntimeException("登记失败：" + e.getMessage(), e);
        }
    }
}
